
import { Painter } from '../view/jaszUniverse/painter';

function createPendant(context) {
  let t0 = new P(context);
  let pendant = new context.Pendant({
    require : ['three'],
    init : (libs, args) => t0.init(libs, args),
    destroy : () => t0.destroy(),

    // custom function
    collectInfo : () => t0.collectInfo(),
    onMessage : (key, value) => t0.onMessage(key, value),
  });
  t0.sendStatus = () => pendant.sendStatus();
  return pendant;
}

/**
 * 初始化参数包括:
 * 
 * dom: 在哪个 div 下建立 canvas
 */
class P {
  constructor(context) {
    global.p = this;
    this.context = context;
    this.tick = this.tick.bind(this);
  }
  
  init(libs, args) {
    this.painter = new Painter({
      three : libs.three,
      dom : args.dom,
      zIndex : -10,
    });

    requestAnimationFrame(this.tick);
  }

  tick() {
    requestAnimationFrame(this.tick);
    this.painter.render();
  }

  destroy() {}

  collectInfo() {
    return {};
  }

  onMessage(key, value) {
    console.log('jaszUniverse 收到信息', key, value);
  }

  sendStatus = () => {};
}

export default createPendant;
